﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using WindowsFormsApplication1.RebornLogin;

namespace WindowsFormsApplication1.TransferCharacterRequests
{
    public class TransferCharacterRequest
    {
        public ServerSocketData transferFromServer;
        public ServerSocketData transferToServer;

        public int characterID;
        public int userID;
        public string clientId;
        public int MapID;
        public int Instance;
        public int MapX;
        public int MapY;
        public string uid;

        private Timer requestTimeout = new Timer(10000);

        public TransferCharacterRequest() 
        {
            requestTimeout.AutoReset = false;
            requestTimeout.Elapsed += new ElapsedEventHandler(requestTimeout_Elapsed);
            requestTimeout.Start();
        }

        private void requestTimeout_Elapsed(object sender, ElapsedEventArgs e)
        {
            Managers.transferCharacterManager.ProcessTransferCharacterResult(this, false);
        }

        private void killTimeout()
        {
            requestTimeout.Stop();
            requestTimeout.Dispose();
        }

        public void processResult(bool result)
        {
            killTimeout();
            try
            {
                if (result == true)
                {
                    string replyMessage = "<transfercharacter_result userid=\"" + userID.ToString() + "\" error=\"" + "" + "\" uid=\"" + uid + "\" port=\"" + transferToServer.clientConnectPort.ToString() + "\" />";
                    SocketConnectionsManager.sendToServer(transferFromServer, replyMessage);
                }
                else
                {
                    string replyMessage = "<transfercharacter_result userid=\"" + userID.ToString() + "\" error=\"" + "Map is unavailable" + "\" uid=\"" + uid + "\" port=\"" + "-1" + "\" />";
                    SocketConnectionsManager.sendToServer(transferFromServer, replyMessage);
                }
            }
            catch { }
        }
    }
}
